package cn.nexttec.noteplayer.ui.adapters

import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import cn.nexttec.noteplayer.databinding.ItemVideoFileBriefBinding
import cn.nexttec.noteplayer.models.VideoFileInfo

class PlaylistVideoAdapter (val onItemClick:(VideoFileInfo)->Unit):RecyclerView.Adapter<PlaylistVideoAdapter.ViewHolder>(){

    private val files = ArrayList<VideoFileInfo>()
    private var currentIndex = 0

    inner class ViewHolder(val binding:ItemVideoFileBriefBinding):RecyclerView.ViewHolder(binding.root){
        fun bind(file:VideoFileInfo, isCurrent:Boolean){
            binding.tvName.text = file.nameWithoutExension
            if (isCurrent){
                binding.tvName.setTextColor(Color.BLUE)
            } else {
                binding.tvName.setTextColor(Color.BLACK)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemVideoFileBriefBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int = files.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val file = files[position]
        holder.bind(file, position == currentIndex)
        holder.binding.root.setOnClickListener {
            onItemClick(file)
        }
    }

    fun setPlayList(current: VideoFileInfo, list: List<VideoFileInfo>) {
        currentIndex = list.indexOf(current)
        files.clear()
        files.addAll(list)
        notifyDataSetChanged()
    }
}